<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>组件间通信 vue2.x 通过bus实现</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../style.css">
</head>

<body>
    <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

    <div id="app">
        {{ message }}
        <my-component-a></my-component-a>
    </div>

    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
    <script src="" async defer></script>

    <script>
        var bus = new Vue()

        Vue.component('my-component-a', {
            template: '<div class="component">\
                    <button @click="handleEvent">传递事件</button>\
                    </div>',
            methods: {
                handleEvent: function () {
                    bus.$emit('on-message', '来自组件my-component-a的内容')
                }
            }
        })

        var handleMounted = function () {
            var _this = this
            //在实例初始化时，监听来自bus实例的事件
            bus.$on('on-message', function (msg) {
                _this.message = msg
            })
        }

        var app = new Vue({
            el: '#app',
            data: {
                message: ''
            },
            mounted: handleMounted
        })
    </script>
</body>

</html>